/**
 * created by 小卷毛, 2020/9/15
 * Copyright (c) 2020, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.sgcc.pda.mdrh.task.safe.util;

import android.content.Context;
import android.os.RemoteException;
import android.text.TextUtils;

import com.sgcc.pda.lib_tools_database.bean.other.VersionItem;
import com.sgcc.pda.lib_tools_database.bean.ticket.TicketMeeting;
import com.sgcc.pda.lib_tools_database.bean.ticket.TicketPermit;
import com.sgcc.pda.lib_tools_database.util.InputParam;
import com.sgcc.pda.mdrh.task.safe.ui.SafeApplication;
import com.sgcc.pda.mdrh.utils.ToolGson2;
import com.sgcc.pda.sdk.utils.DataConversionUtil;
import com.sgcc.pda.sdk.utils.db.SharepreferenceUtil;
import com.sgcc.pda.web.IHttpCallbackInterface;
import com.sgcc.pda.web.service.ZoomWebControler;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 描 述：网络请求工具类
 * 作 者：小卷毛  2020-09-15 17:06
 * 修改描述：接口增加查询参数
 * 修 改 人：
 * 修改版本：v1.2.1
 */
public class HttpUtil {

    public static final String SERVICE_NAME = "safemanage-external-service";
    public static final String SERVICE_NAME2 = "safemanage-service";

    /**
     * 知识库版本更新检查
     *
     * @param context  上下文
     * @param callback 异步结果回调
     */
    public static void getVersonData(
            Context context,
            IHttpCallbackInterface callback
    ) {
        //未登录时不做请求
        if (TextUtils.isEmpty(SharepreferenceUtil.getUid(context)) ||
                TextUtils.isEmpty(SharepreferenceUtil.getEncryptionMode(context))) {
            return;
        }

        try {
            Map<String, String> requestParams = new HashMap<>();
            requestParams.put("uid", SharepreferenceUtil.getUid(SafeApplication.getInstance()));
            requestParams.put("tables", new InputParam().getArrayParam());

            String params = weaveString(
                    SERVICE_NAME,
                    "safemanagement/knowledgeVersionCheck?",
                    requestParams
            );

            ZoomWebControler.getInstance()
                    .postDataCallback(
                            context,
                            "commFront.do",
                            params,
                            callback
                    );
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 知识库数据表更新下载
     *
     * @param context  上下文
     * @param data     参数
     * @param callback 异步回调
     */
    public static void updateVersonData(
            Context context,
            VersionItem data,
            IHttpCallbackInterface callback
    ) {
        try {
            Map<String, String> requestParams = new HashMap<>();
            requestParams.put("uid", SharepreferenceUtil.getUid(SafeApplication.getInstance()));
            requestParams.put("tableName", data.getTableName());
            requestParams.put("versionNo", data.getVersionNo());
            requestParams.put("timestamp", data.getTimestamp());

            String params = weaveString(
                    SERVICE_NAME,
                    "safemanagement/knowledgeDownload?",
                    requestParams
            );

            ZoomWebControler.getInstance()
                    .postDataCallback(
                            context,
                            "commFront.do",
                            params,
                            callback
                    );
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 知识库数据表同步更新下载
     *
     * @param context  上下文
     * @param data     参数
     * @param callback 同步回调
     */
    public static void updateVersonDataSync(
            Context context,
            VersionItem data,
            IHttpCallbackInterface callback
    ) {
        //未登录时不做请求
        if (TextUtils.isEmpty(SharepreferenceUtil.getUid(context)) ||
                TextUtils.isEmpty(SharepreferenceUtil.getEncryptionMode(context))) {
            return;
        }

        try {
            Map<String, String> requestParams = new HashMap<>();
            requestParams.put("uid", SharepreferenceUtil.getUid(SafeApplication.getInstance()));
            requestParams.put("tableName", data.getTableName());
            requestParams.put("versionNo", data.getVersionNo());
            requestParams.put("timestamp", data.getTimestamp());

            String params = weaveString(
                    SERVICE_NAME,
                    "safemanagement/knowledgeDownload?",
                    requestParams
            );

            ZoomWebControler.getInstance()
                    .postDataSync(
                            context,
                            "commFront.do",
                            params,
                            callback
                    );
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取工作票状态（工作计划）
     *
     * @param context     上下文
     * @param appNo       工单号
     * @param planNo      工作计划编号
     * @param appCode     业务应用编码
     * @param eomOrderSrc 工单来源系统编号
     * @param workTypeNo  工单类型编号
     * @param listener    回调监听
     */
    public static void getTicketStateByPlan(
            Context context,
            String appNo,
            String planNo,
            String appCode,
            String eomOrderSrc,
            String workTypeNo,
            IHttpCallbackInterface listener
    ) {
        Map<String, String> requestParams = new HashMap<>();
        requestParams.put("appNo", appNo);
        requestParams.put("planNo", planNo);
        requestParams.put("appCode", appCode);
        requestParams.put("eomOrderSrc", eomOrderSrc);
        requestParams.put("workTypeNo", workTypeNo);
        try {
            String params = weaveString(SERVICE_NAME, "safemanagement/workTicketStatusByPlan?", requestParams);
            ZoomWebControler.getInstance().postDataCallback(context, "commFront.do", params, listener);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取班前会状态（工作计划）
     *
     * @param context     上下文
     * @param appNo       工单号
     * @param planNo      工作计划编号
     * @param appCode     业务应用编码
     * @param eomOrderSrc 工单来源系统编号
     * @param workTypeNo  工单类型编号
     * @param listener    回调监听
     */
    public static void getMeetingStateByPlan(
            Context context,
            String appNo,
            String planNo,
            String appCode,
            String eomOrderSrc,
            String workTypeNo,
            IHttpCallbackInterface listener
    ) {
        Map<String, String> requestParams = new HashMap<>();
        requestParams.put("appNo", appNo);
        requestParams.put("planNo", planNo);
        requestParams.put("appCode", appCode);
        requestParams.put("eomOrderSrc", eomOrderSrc);
        requestParams.put("workTypeNo", workTypeNo);
        try {
            String params = weaveString(SERVICE_NAME, "safemanagement/siteMeetingStateByPlan?", requestParams);
            ZoomWebControler.getInstance().postDataCallback(context, "commFront.do", params, listener);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 申请终结工作票接口（工作计划）
     *
     * @param context     上下文
     * @param appNo       工单号
     * @param planNo      工作票号
     * @param appCode     业务应用编码
     * @param eomOrderSrc 工单来源系统编号
     * @param workTypeNo  工单类型编号
     * @param listener    回调监听
     */
    public static void applyCloseTicketCheckByPlan(
            Context context,
            String appNo,
            String planNo,
            String appCode,
            String eomOrderSrc,
            String workTypeNo,
            IHttpCallbackInterface listener
    ) {
        Map<String, String> requestParams = new HashMap<>();
        requestParams.put("appNo", appNo);
        requestParams.put("planNo", planNo);
        requestParams.put("appCode", appCode);
        requestParams.put("eomOrderSrc", eomOrderSrc);
        requestParams.put("workTypeNo", workTypeNo);
        try {
            String params = weaveString(SERVICE_NAME, "safemanagement/applyCloseTicketCheckByPlan?", requestParams);
            ZoomWebControler.getInstance().postDataCallback(context, "commFront.do", params, listener);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取作业监控工作计划列表
     *
     * @param context  上下文
     * @param listener 回调监听
     */
    public static void getMonitorPlanList(Context context, IHttpCallbackInterface listener) {
        Map<String, String> requestParams = new HashMap<>();
        requestParams.put("orgNo", SharepreferenceUtil.getOrgCode(context));
        try {
            String params = weaveString(SERVICE_NAME, "safemanagement/safeMiniotrApplyPlanList?", requestParams);
            ZoomWebControler.getInstance().postDataCallback(context, "commFront.do", params, listener);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 作业安全监控数据提交完成
     *
     * @param context  上下文
     * @param listener 回调监听
     */
    public static void sendMonitorPlanData(Context context, IHttpCallbackInterface listener) {
        Map<String, String> requestParams = new HashMap<>();
        requestParams.put("uid", SharepreferenceUtil.getUid(context));
        try {
            String params = weaveString(SERVICE_NAME, "safemanagement/qsafeMiniotrSendMiniotrData?", requestParams);
            ZoomWebControler.getInstance().postDataCallback(context, "commFront.do", params, listener);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询工作票关联防范措施
     *
     * @param context  上下文
     * @param wtNo     工作票编号
     * @param listener 回调监听
     */
    public static void getMeasuresByTicket(Context context, String wtNo, IHttpCallbackInterface listener) {
        Map<String, String> requestParams = new HashMap<>();
        requestParams.put("wtNo", wtNo);
        try {
            String params = weaveString(SERVICE_NAME, "safemanagement/riskPointEviByWT?", requestParams);
            ZoomWebControler.getInstance().postDataCallback(context, "commFront.do", params, listener);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 保存防范措施取证记录
     *
     * @param context  上下文
     * @param appNo    业务工单号
     * @param planNo   工作计划编号
     * @param wtNo     工作票编号
     * @param measures 防范措施(jsonArray形式)
     * @param listener 回调监听
     */
    public static void saveTicketRiskRecord(
            Context context,
            String appNo,
            String planNo,
            String wtNo,
            String isChk,
            String measures,
            IHttpCallbackInterface listener
    ) {
        Map<String, String> requestParams = new HashMap<>();
        requestParams.put("appNo", appNo);
        requestParams.put("planNo", planNo);
        requestParams.put("wtNo", wtNo);
        requestParams.put("isChk", isChk);
        requestParams.put("measures", measures);
        try {
            String params = weaveString(SERVICE_NAME, "safemanagement/saveRiskPointEvi?", requestParams);
            ZoomWebControler.getInstance().postDataSync(context, "commFront.do", params, listener);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 异步保存防范措施取证记录
     *
     * @param context  上下文
     * @param appNo    业务工单号
     * @param planNo   工作计划编号
     * @param wtNo     工作票编号
     * @param measures 防范措施(jsonArray形式)
     * @param listener 回调监听
     */
    public static void saveTicketRiskRecordAsync(
            Context context,
            String appNo,
            String planNo,
            String wtNo,
            String isChk,
            String measures,
            IHttpCallbackInterface listener
    ) {
        Map<String, String> requestParams = new HashMap<>();
        requestParams.put("appNo", appNo);
        requestParams.put("planNo", planNo);
        requestParams.put("wtNo", wtNo);
        requestParams.put("isChk", isChk);
        requestParams.put("measures", measures);
        try {
            String params = weaveString(SERVICE_NAME, "safemanagement/saveRiskPointEvi?", requestParams);
            ZoomWebControler.getInstance().postDataCallback(context, "commFront.do", params, listener);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 离线查询待许可工作票、班前会、题库信息
     *
     * @param context  上下文
     * @param listener 回调监听
     */
    public static void getLocalTicketDetail(
            Context context,
            IHttpCallbackInterface listener
    ) {
        Map<String, String> requestParams = new HashMap<>();
        requestParams.put("UID", SharepreferenceUtil.getUid(SafeApplication.getInstance()));
        requestParams.put("wtState", "03,04");
        try {
            String params = weaveString(SERVICE_NAME2, "ticket/workTicketByUid?", requestParams);
            ZoomWebControler.getInstance().postDataCallback(context, "commFront.do", params, listener);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 离线查询单个待许可工作票、班前会、题库信息
     *
     * @param context  上下文
     * @param recordId 工作票Id
     * @param listener 回调监听
     */
    public static void getSingleTicketDetail(
            Context context,
            String recordId,
            IHttpCallbackInterface listener
    ) {
        Map<String, String> requestParams = new HashMap<>();
        requestParams.put("UID", SharepreferenceUtil.getUid(SafeApplication.getInstance()));
        requestParams.put("RECORD_ID", recordId);
        requestParams.put("wtState", "03,04");
        try {
            String params = weaveString(SERVICE_NAME2, "ticket/workTicketByUid?", requestParams);
            ZoomWebControler.getInstance().postDataCallback(context, "commFront.do", params, listener);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取工作票类型与设施列表
     *
     * @param context  上下文
     * @param listener 回调监听
     */
    public static void getTicketFaciTypes(
            Context context,
            IHttpCallbackInterface listener
    ) {
        Map<String, String> requestParams = new HashMap<>();
        requestParams.put("UID", SharepreferenceUtil.getUid(SafeApplication.getInstance()));
        try {
            String params = weaveString(SERVICE_NAME2, "ticket/getFaciTypes?", requestParams);
            ZoomWebControler.getInstance().postDataCallback(context, "commFront.do", params, listener);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 离线许可工作票信息提交
     *
     * @param context  上下文
     * @param param    提交参数
     * @param listener 回调监听
     */
    public static void submitTicketPermit(
            Context context,
            TicketPermit param,
            IHttpCallbackInterface listener
    ) {
        Map<String, String> requestParams = new HashMap<>();
        requestParams.put("RECORD_ID", param.getRECORD_ID());
        requestParams.put("WT_RECORD_WAY", param.getWT_RECORD_WAY());
        requestParams.put("WT_PERMIT_PER_NAME", param.getWT_PERMIT_PER_NAME());
        requestParams.put("WT_ALLOW_TIME", param.getWT_ALLOW_TIME());
        requestParams.put("WT_ALLOW_WAY", param.getWT_ALLOW_WAY());
        requestParams.put("WT_CUSTOMER_NAME", param.getWT_CUSTOMER_NAME());
        requestParams.put("WT_SAFE_MEASURE_ADD", param.getWT_SAFE_MEASURE_ADD());
        requestParams.put("signDataList", ToolGson2.getJsonStr(param.getSignDataList()));
        try {
            String params = weaveString(SERVICE_NAME2, "ticket/permit?", requestParams);
            ZoomWebControler.getInstance().postDataCallback(context, "commFront.do", params, listener);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 离线许可工作票信息提交（同步）
     *
     * @param context  上下文
     * @param param    提交参数
     * @param listener 回调监听
     */
    public static void submitTicketPermitSync(
            Context context,
            TicketPermit param,
            IHttpCallbackInterface listener
    ) {
        Map<String, String> requestParams = new HashMap<>();
        requestParams.put("RECORD_ID", param.getRECORD_ID());
        requestParams.put("WT_RECORD_WAY", param.getWT_RECORD_WAY());
        requestParams.put("WT_PERMIT_PER_NAME", param.getWT_PERMIT_PER_NAME());
        requestParams.put("WT_ALLOW_TIME", param.getWT_ALLOW_TIME());
        requestParams.put("WT_ALLOW_WAY", param.getWT_ALLOW_WAY());
        requestParams.put("WT_CUSTOMER_NAME", param.getWT_CUSTOMER_NAME());
        requestParams.put("WT_SAFE_MEASURE_ADD", param.getWT_SAFE_MEASURE_ADD());
        requestParams.put("signDataList", ToolGson2.getJsonStr(param.getSignDataList()));
        try {
            String params = weaveString(SERVICE_NAME2, "ticket/permit?", requestParams);
            ZoomWebControler.getInstance().postDataSync(context, "commFront.do", params, listener);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 离线终结工作票信息提交
     *
     * @param context  上下文
     * @param param    提交参数
     * @param listener 回调监听
     */
    public static void submitTicketEnd(
            Context context,
            TicketPermit param,
            IHttpCallbackInterface listener
    ) {
        Map<String, String> requestParams = new HashMap<>();
        requestParams.put("RECORD_ID", param.getRECORD_ID());
        requestParams.put("WT_RECORD_WAY", param.getWT_RECORD_WAY());
        requestParams.put("WT_PERMIT_PER_NAME", param.getWT_PERMIT_PER_NAME());
        requestParams.put("WT_ALLOW_TIME", param.getWT_ALLOW_TIME());
        requestParams.put("WT_ALLOW_WAY", param.getWT_ALLOW_WAY());
        requestParams.put("WT_CUSTOMER_NAME", param.getWT_CUSTOMER_NAME());
        requestParams.put("WT_SAFE_MEASURE_ADD", param.getWT_SAFE_MEASURE_ADD());
        requestParams.put("WT_FILD_PATHS", param.getWT_FILD_PATHS());
        requestParams.put("WT_END_REMARK", param.getWT_END_REMARK());
        requestParams.put("faciDataList", param.getFaciDataList());
        requestParams.put("signDataList", ToolGson2.getJsonStr(param.getSignDataList()));
        try {
            String params = weaveString(SERVICE_NAME2, "ticket/end?", requestParams);
            ZoomWebControler.getInstance().postDataCallback(context, "commFront.do", params, listener);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 离线终结工作票信息提交（同步）
     *
     * @param context  上下文
     * @param param    提交参数
     * @param listener 回调监听
     */
    public static void submitTicketEndSync(
            Context context,
            TicketPermit param,
            IHttpCallbackInterface listener
    ) {
        Map<String, String> requestParams = new HashMap<>();
        requestParams.put("RECORD_ID", param.getRECORD_ID());
        requestParams.put("WT_RECORD_WAY", param.getWT_RECORD_WAY());
        requestParams.put("WT_PERMIT_PER_NAME", param.getWT_PERMIT_PER_NAME());
        requestParams.put("WT_ALLOW_TIME", param.getWT_ALLOW_TIME());
        requestParams.put("WT_ALLOW_WAY", param.getWT_ALLOW_WAY());
        requestParams.put("WT_CUSTOMER_NAME", param.getWT_CUSTOMER_NAME());
        requestParams.put("WT_SAFE_MEASURE_ADD", param.getWT_SAFE_MEASURE_ADD());
        requestParams.put("WT_FILD_PATHS", param.getWT_FILD_PATHS());
        requestParams.put("WT_END_REMARK", param.getWT_END_REMARK());
        requestParams.put("faciDataList", param.getFaciDataList());
        requestParams.put("signDataList", ToolGson2.getJsonStr(param.getSignDataList()));
        try {
            String params = weaveString(SERVICE_NAME2, "ticket/end?", requestParams);
            ZoomWebControler.getInstance().postDataCallback(context, "commFront.do", params, listener);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 离线结束班前会信息提交
     *
     * @param context  上下文
     * @param param    提交参数
     * @param listener 回调监听
     */
    public static void submitMeetingEnd(
            Context context,
            TicketMeeting param,
            IHttpCallbackInterface listener
    ) {
        Map<String, String> requestParams = new HashMap<>();
        requestParams.put("WT_ID", param.getWT_ID());
        requestParams.put("START_TIME", param.getSTART_TIME());
        requestParams.put("END_TIME", param.getEND_TIME());
        requestParams.put("FILE_LIST", ToolGson2.getJsonStr(param.getFILE_LIST()));
        requestParams.put("ITEMS", param.getITEMS_STRING());
        requestParams.put("SIGNS", ToolGson2.getJsonStr(param.getSIGNS()));
        try {
            String params = weaveString(SERVICE_NAME2, "siteMeeting/endSiteMeeting?", requestParams);
            ZoomWebControler.getInstance().postDataCallback(context, "commFront.do", params, listener);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * OSS鉴权STS
     *
     * @param context  上下文
     * @param listener 回调监听
     */
    public static void getOSSTokenSync(
            Context context,
            IHttpCallbackInterface listener
    ) {
        Map<String, String> requestParams = new HashMap<>();
        requestParams.put("uid", SharepreferenceUtil.getUid(context));
        try {
            String params = weaveString(SERVICE_NAME2, "common/getUploadWay?", requestParams);
            ZoomWebControler.getInstance().postDataSync(context, "commFront.do", params, listener);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * OSS鉴权STS同步
     *
     * @param context  上下文
     * @param listener 回调监听
     */
    public static void getOSSToken(
            Context context,
            IHttpCallbackInterface listener
    ) {
        Map<String, String> requestParams = new HashMap<>();
        requestParams.put("uid", SharepreferenceUtil.getUid(context));
        try {
            String params = weaveString(SERVICE_NAME2, "common/getUploadWay?", requestParams);
            ZoomWebControler.getInstance().postDataCallback(context, "commFront.do", params, listener);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 离线结束班前会信息提交（同步）
     *
     * @param context  上下文
     * @param param    提交参数
     * @param listener 回调监听
     */
    public static void submitMeetingEndSync(
            Context context,
            TicketMeeting param,
            IHttpCallbackInterface listener
    ) {
        Map<String, String> requestParams = new HashMap<>();
        requestParams.put("WT_ID", param.getWT_ID());
        requestParams.put("START_TIME", param.getSTART_TIME());
        requestParams.put("END_TIME", param.getEND_TIME());
        requestParams.put("FILE_LIST", ToolGson2.getJsonStr(param.getFILE_LIST()));
        requestParams.put("ITEMS", param.getITEMS_STRING());
        requestParams.put("SIGNS", ToolGson2.getJsonStr(param.getSIGNS()));
        try {
            String params = weaveString(SERVICE_NAME2, "siteMeeting/endSiteMeeting?", requestParams);
            ZoomWebControler.getInstance().postDataCallback(context, "commFront.do", params, listener);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 拼装参数
     */
    private static String weaveString(String serverName, String cmdName, Map<String, String> requestParams) {
        StringBuilder builder = new StringBuilder();
        builder.append(cmdName);
        for (String key : requestParams.keySet()) {
            builder.append(key).append("=").append(requestParams.get(key)).append("&");
        }
        JSONObject jb = new JSONObject();
        try {
            String url = builder.toString();
            String paramsHex = DataConversionUtil.str2HexStr(url.substring(0, url.length() - 1));
            jb.put("SERVER_NAME", serverName);
            jb.put("REQUEST_PARM", paramsHex);
            jb.put("UID", SharepreferenceUtil.getUid(SafeApplication.getInstance()));
            jb.put("MAC", "12345");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jb.toString();
    }

}
